package com.fz.us.oa.core.dao.com.impl;

import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.result.SearchResultList;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.bean.search.rule.RuleHelper;
import com.fz.us.admin.company.dao.impl.BaseFlowDaoImpl;
import com.fz.us.admin.company.service.UserService;
import com.fz.us.admin.dict.entity.DictItem;
import com.fz.us.base.bean.BaseEnum;
import com.fz.us.oa.core.dao.com.DirectorDao;
import com.fz.us.oa.core.entity.com.Director;
import com.fz.us.oa.core.entity.com.Person;
import com.fz.us.oa.core.service.com.DirectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dzw on 2018/3/19.
 */
@Repository
public class DirectorDaoImpl extends BaseFlowDaoImpl<Director, String> implements DirectorDao {

    @Autowired
    private UserService userService;
    @Autowired
    private DirectorService directorService;

    @Override
    public List<Director> findPerson(List<Rule> rules) {
        if(rules==null){
            rules = new ArrayList<Rule>();
        }
        Order order = Order.order("pinYinHead", SearchEnum.OrderType.asc);

        rules.add(RuleHelper.eq("state", BaseEnum.StateEnum.Enable));

        SearchResultList searchResult = directorService.findListBySearch(rules, order);
        List<Director> directorList = searchResult.getList();
        return directorList;
    }

    @Override
    public int getCountEduAll(DictItem edu) {
        Object count = getSession().createQuery("select count(*) from Director p where p.state=? and p.highEdu=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, edu)
                .uniqueResult();
        return count!=null ? Integer.parseInt(count.toString()) : 0;
    }

    @Override
    public int getCountEduAll(DictItem edu, DictItem street) {
        Object count = getSession().createQuery("select count(*) from Director p where p.state=? and p.highEdu=? and p.street=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, edu)
                .setParameter(2, street)
                .uniqueResult();
        return count!=null ? Integer.parseInt(count.toString()) : 0;
    }

    @Override
    public int getCountByPersonNature(DictItem personNature) {
        Object count = getSession().createQuery("select count(*) from Director p where p.state=? and p.personNature=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, personNature)
                .uniqueResult();
        return count!=null ? Integer.parseInt(count.toString()) : 0;
    }

    @Override
    public int getCountByNatureAndStreet(DictItem personNature, DictItem street) {
        Object count = getSession().createQuery("select count(*) from Director p where p.state=? and p.personNature=? and p.street=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, personNature)
                .setParameter(2, street)
                .uniqueResult();
        return count!=null ? Integer.parseInt(count.toString()) : 0;
    }

    @Override
    public int getAllCount() {
        Object count = getSession().createQuery("select count(*) from Director p where p.state=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .uniqueResult();
        return count==null?0:Integer.parseInt(count.toString());
    }

    @Override
    public int getCountByStreet(DictItem street) {
        Object count = getSession().createQuery("select count(*) from Director p where p.state=? and p.street=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, street)
                .uniqueResult();
        return count!=null ? Integer.parseInt(count.toString()) : 0;
    }

    @Override
    public int getCountByGender(String gender) {
        Object count = getSession().createQuery("select count(*) from Director p where p.state=? and p.gender=?")
                .setParameter(0, BaseEnum.StateEnum.Enable)
                .setParameter(1, gender)
                .uniqueResult();
        return count!=null ? Integer.parseInt(count.toString()) : 0;
    }

    @Override
    public Director getByKeyId(String keyId) {
        List<Director> directorList = getSession().createQuery("from Director p where p.keyId=?")
                .setParameter(0, keyId)
                .list();
        if(directorList.size()>0){
            return directorList.get(0);
        }else{
            return null;
        }
    }
}
